home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / rmold / RCS / rmold.c,v < prev    next >
Encoding:
Text File  |  1991-03-12  |  4.8 KB  |  222 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     91.03.12.13.52.57;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     90.03.31.16.22.14;  author ouster;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @Add "timesFrom" option.  Enable use of function prototypes.  lint.
  27. @
  28. text
  29. @/* 
  30.  * rmold.c --
  31.  *
  32.  *    Program to remove files older than a certain number of days.
  33.  *
  34.  * Copyright 1990 Regents of the University of California
  35.  * Permission to use, copy, modify, and distribute this
  36.  * software and its documentation for any purpose and without
  37.  * fee is hereby granted, provided that the above copyright
  38.  * notice appear in all copies.  The University of California
  39.  * makes no representations about the suitability of this
  40.  * software for any purpose.  It is provided "as is" without
  41.  * express or implied warranty.
  42.  */
  43.  
  44. #ifndef lint
  45. static char rcsid[] = "$Header: /sprite/src/admin/rmold/RCS/rmold.c,v 1.1 90/03/31 16:22:14 ouster Exp Locker: kupfer $ SPRITE (Berkeley)";
  46. #endif /* not lint */
  47.  
  48. #ifndef ds3100
  49. #ifndef lint
  50. #define _HAS_PROTOTYPES
  51. #define _HAS_VOIDPTR
  52. #endif
  53. #endif
  54.  
  55. #include <sprite.h>
  56. #include <errno.h>
  57. #include <option.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <sys/types.h>
  62. #include <sys/stat.h>
  63. #include <sys/time.h>
  64. #include <unistd.h>
  65.  
  66. /*
  67.  * Command-line options:
  68.  */
  69.  
  70. int delete = 1;
  71. int mod = 0;        /* 1 means use modify time for deletion, 0 means
  72.              * use access time. */
  73. char *timesDirectory = ".";    /* directory to get mod/access times from */
  74.  
  75. Option optionArray[] = {
  76.     OPT_DOC,    (char *) NULL,    (char *) NULL,
  77.         "This program deletes files that haven't been modifed recently.\n Synopsis: \"rmold [-print] numDays file file ...\"\n Command-line switches are:",
  78.     OPT_TRUE,    "mod",        (char *) &mod,
  79.         "Use modify time instead of access time",
  80.     OPT_FALSE,    "print",    (char *) &delete,
  81.         "Don't delete, just print file names that would be deleted",
  82.     OPT_STRING, "timesFrom",    (char *)×Directory,
  83.         "Name the directory to determine the access or modify time.",
  84. };
  85.  
  86. static char *AllocSpace _ARGS_ ((int numNames, char *namesArray[]));
  87.  
  88.  
  89. int
  90. main(argc, argv)
  91.     int argc;
  92.     char **argv;
  93. {
  94.     int i;
  95.     u_long cutoffTime;
  96.     struct timeval now;
  97.     struct timezone dummy;
  98.     char *end;
  99.     struct stat buf;
  100.     char *fileName;        /* file name for getting access/mod time */
  101.  
  102.     /*
  103.      * Check arguments and compute the cutoff time.
  104.      */
  105.  
  106.     argc = Opt_Parse(argc, argv, optionArray, Opt_Number(optionArray), 0);
  107.     if (argc < 3) {
  108.     fprintf(stderr, "Usage: %s numDays file file ...\n", argv[0]);
  109.     exit(1);
  110.     }
  111.     i = strtoul(argv[1], &end, 0);
  112.     if (end == argv[1]) {
  113.     badArg:
  114.     fprintf(stderr, "Bad \"numDays\" argument \"%s\"\n", argv[1]);
  115.     exit(1);
  116.     }
  117.     i *= 24*3600;
  118.     gettimeofday(&now, &dummy);
  119.     if (i > now.tv_sec) {
  120.     goto badArg;
  121.     }
  122.     cutoffTime = now.tv_sec - i;
  123.  
  124.     fileName = AllocSpace(argc-2, argv+2);
  125.  
  126.     /*
  127.      * Scan through all of the files and delete any that haven't been
  128.      * accessed since the cutoff time.
  129.      */
  130.  
  131.     for (i = 2; i < argc; i++) {
  132.     strcpy(fileName, timesDirectory);
  133.     strcat(fileName, "/");
  134.     strcat(fileName, argv[i]);
  135.     if (stat(fileName, &buf) != 0) {
  136.         fprintf(stderr, "Couldn't stat \"%s\": %s\n",
  137.             fileName, strerror(errno));
  138.         continue;
  139.     }
  140.     if (mod) {
  141.         if (buf.st_mtime > cutoffTime) {
  142.         continue;
  143.         }
  144.     } else {
  145.         if (buf.st_atime > cutoffTime) {
  146.         continue;
  147.         }
  148.     }
  149.     if (delete && (unlink(argv[i]) != 0)) {
  150.         fprintf(stderr, "Couldn't remove \"%s\": %s\n",
  151.             argv[i], strerror(errno));
  152.         continue;
  153.     }
  154.     printf("%s\n", argv[i]);
  155.     }
  156.  
  157.     exit(0);
  158.     return 0;            /* suppress -Wall complaint */
  159. }
  160.  
  161.  
  162. /*
  163.  *----------------------------------------------------------------------
  164.  *
  165.  * AllocSpace --
  166.  *
  167.  *    Allocate a character buffer large enough for all the file names.
  168.  *
  169.  * Results:
  170.  *    A character array large enough to hold the concatentation of 
  171.  *    the search directory, "/", and any of the arguments.
  172.  *
  173.  * Side effects:
  174.  *    None.
  175.  *
  176.  *----------------------------------------------------------------------
  177.  */
  178.  
  179. static char *
  180. AllocSpace(numNames, namesArray)
  181.     int numNames;
  182.     char *namesArray[];        /* list of file names */
  183. {
  184.     int i;            /* index into argv */
  185.     int maxLength = 0;        /* longest length in namesArray */
  186.  
  187.     for (i = 0; i < numNames; i++) {
  188.     if (strlen(namesArray[i]) > maxLength) {
  189.         maxLength = strlen(namesArray[i]);
  190.     }
  191.     }
  192.  
  193.     return malloc(strlen(timesDirectory) + strlen("/") + maxLength + 1);
  194. }
  195. @
  196.  
  197.  
  198. 1.1
  199. log
  200. @Initial revision
  201. @
  202. text
  203. @d17 1
  204. a17 1
  205. static char rcsid[] = "$Header: /sprite/lib/forms/RCS/proto.c,v 1.3 90/01/12 12:03:36 douglis Exp $ SPRITE (Berkeley)";
  206. d20 8
  207. d36 1
  208. d45 1
  209. d54 2
  210. d58 2
  211. d61 1
  212. d72 1
  213. d96 2
  214. d104 4
  215. a107 1
  216.     if (stat(argv[i], &buf) != 0) {
  217. d109 1
  218. a109 1
  219.             argv[i], strerror(errno));
  220. d130 36
  221. @
  222.